package com.main.by.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.main.by.entity.vo.SubmitAndTaskVO;
import com.main.by.exception.ServiceException;
import com.main.by.service.SubmitAndTaskService;
import com.main.by.util.StringUtil;

@Controller
@RequestMapping("/adminjsp/")
public class SubmitController {
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	private SubmitAndTaskService submitAndTaskService;
	
	@RequestMapping(value = "pending", method = RequestMethod.GET)
	public String pending(HttpServletRequest request){
		logger.info("查询任务");
		try {
			List<SubmitAndTaskVO> submitAndTask = submitAndTaskService.findAll(request);
			if (submitAndTask == null) {
				return "adminjsp/pending";
			}
			request.getSession().setAttribute("submitandTaskInfoList", submitAndTask);
		}catch (ServiceException serviceE){
			return "adminjsp/index";
		}catch (Exception e) {
			logger.error("select news failed! ", e);
			return "adminjsp/index";
		}
		
		return "adminjsp/pending";
	}
	
	@RequestMapping(value = "pendingTask", method = RequestMethod.GET)
	public String pendingTask(HttpServletRequest request){
		logger.info("查看详细的任务信息");
		String errorMessage = "修改失败";
		String submitId = request.getParameter("submitId");
		SubmitAndTaskVO submitAndTaskinfo = new SubmitAndTaskVO();
		List<SubmitAndTaskVO> submitAndTask = (List<SubmitAndTaskVO>)request.getSession().getAttribute("submitandTaskInfoList");
		if (submitAndTask.size() >= 1) {
			for (int i =0 ; i < submitAndTask.size(); i++) {
				if (StringUtil.isNotBlank(submitId) && submitId.equals(submitAndTask.get(i).getSubmitId())) {
					submitAndTaskinfo = submitAndTask.get(i);
					request.getSession().setAttribute("submitandTaskInfo", submitAndTaskinfo);
					return "adminjsp/pendingTask";
				}
			}
			return "adminjsp/pendingTask";
		}
		return "adminjsp/pendingTask";
	}
	
	//审核用户
	@RequestMapping(value = "pendingSubmit", method = RequestMethod.GET)
	public void pendingSubmit(HttpServletRequest request,HttpServletResponse response) throws IOException{
		logger.info("审核审核任务信息-------");
		String submitId =  request.getParameter("submitId");
		response.setContentType("text/html;charset=utf8");
		PrintWriter out = response.getWriter();
		int id = 0;
		if (StringUtil.isNotBlank(submitId)) {
			id = Integer.parseInt(submitId);
		}
		try{
			submitAndTaskService.auditSubmitInfo(id);
			response.sendRedirect("pending");
		}catch (ServiceException serviceE){
			out
			.print("<script type='text/javascript'>alert('审核失败');history.go(-1);</script>");
		}catch (Exception e) {
			logger.error("select news failed! ", e);
			out
			.print("<script type='text/javascript'>alert('审核失败');history.go(-1);</script>");
		}
	}
	
	@RequestMapping(value = "deleteSubmitInfo", method = RequestMethod.GET)
	public void deleteSubmitInfo(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("utf8");
		response.setContentType("text/html;charset=utf8");
		String submitId =  request.getParameter("submitId");
		PrintWriter out = response.getWriter();
		int id = 0;
		if (StringUtil.isNotBlank(submitId)) {
			id = Integer.parseInt(submitId);
		}
		logger.info("根据ID查找管理员信息-------");
		try {
			submitAndTaskService.deleteSubmitById(id);
			response.sendRedirect("pending");
		}catch (ServiceException serviceE){
			out
			.print("<script type='text/javascript'>alert('删除失败');history.go(-1);</script>");
		}catch (Exception e) {
			logger.error("select admin failed! ", e);
			out
			.print("<script type='text/javascript'>alert('删除失败');history.go(-1);</script>");
		}
		
	}
	
	@RequestMapping(value = "audited", method = RequestMethod.GET)
	public String audited(HttpServletRequest request){
		logger.info("查询任务");
		try {
			List<SubmitAndTaskVO> submitAndTask = submitAndTaskService.findAllAudited(request);
			request.getSession().setAttribute("submitAuditedTaskInfoList", submitAndTask);
		}catch (ServiceException serviceE){
			return "customjsp/index";
		}catch (Exception e) {
			logger.error("select news failed! ", e);
			return "customjsp/index";
		}
		
		return "adminjsp/audited";
	}

	@RequestMapping(value = "auditedTask", method = RequestMethod.GET)
	public String auditedTask(HttpServletRequest request){
		logger.info("查看详细的任务信息");
		String submitId = request.getParameter("submitId");
		SubmitAndTaskVO submitAndTaskinfo = new SubmitAndTaskVO();
		List<SubmitAndTaskVO> submitAndAuditedTask = (List<SubmitAndTaskVO>)request.getSession().getAttribute("submitAuditedTaskInfoList");
		if (submitAndAuditedTask.size() >= 1) {
			for (int i =0 ; i < submitAndAuditedTask.size(); i++) {
				if (StringUtil.isNotBlank(submitId) && submitId.equals(submitAndAuditedTask.get(i).getSubmitId())) {
					submitAndTaskinfo = submitAndAuditedTask.get(i);
					request.getSession().setAttribute("submitAuditedTaskInfo", submitAndTaskinfo);
					return "adminjsp/auditedTask";
				}
			}
			return "adminjsp/auditedTask";
		} 
		return "adminjsp/auditedTask";
	}
	
	@RequestMapping(value = "deleteAuditedSubmitInfo", method = RequestMethod.GET)
	public void deleteAuditedSubmitInfo(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("utf8");
		response.setContentType("text/html;charset=utf8");
		String submitId =  request.getParameter("submitId");
		PrintWriter out = response.getWriter();
		int id = 0;
		if (StringUtil.isNotBlank(submitId)) {
			id = Integer.parseInt(submitId);
		}
		logger.info("根据ID查找管理员信息-------");
		try {
			submitAndTaskService.deleteSubmitById(id);
			response.sendRedirect("audited");
		}catch (ServiceException serviceE){
			out
			.print("<script type='text/javascript'>alert('删除失败');history.go(-1);</script>");
		}catch (Exception e) {
			logger.error("select admin failed! ", e);
			out
			.print("<script type='text/javascript'>alert('删除失败');history.go(-1);</script>");
		}
		
	}
	
}
